package top.seekincs.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.seekincs.entity.AlumniSchoolEntity;
import top.seekincs.repossitory.AlumniSchoolRepository;

import java.util.Objects;
import java.util.Optional;

@Service
public class AlumniSchoolService {

    final AlumniSchoolRepository alumniSchoolRepository;

    public AlumniSchoolService(AlumniSchoolRepository alumniSchoolRepository) {
        this.alumniSchoolRepository = alumniSchoolRepository;
    }

    public AlumniSchoolEntity addSchool(String name, String description) {
        AlumniSchoolEntity alumniSchoolEntity = alumniSchoolRepository.findByName(name);
        if (Objects.isNull(alumniSchoolEntity)) {
            return alumniSchoolRepository.save(
                    AlumniSchoolEntity.builder()
                            .name(name)
                            .description(description)
                            .build()
            );
        }
        return alumniSchoolEntity;
    }

    public Integer findIdByName(String name) {
        if (Objects.isNull(name)) {
            return null;
        }
        AlumniSchoolEntity schoolEntity = alumniSchoolRepository.findByName(name);
        return schoolEntity == null ? 0 : schoolEntity.getId();
    }

    public AlumniSchoolEntity findById(Integer schoolId) {
        Optional<AlumniSchoolEntity> schoolEntity = alumniSchoolRepository.findById(schoolId);
        return schoolEntity.orElseGet(() -> AlumniSchoolEntity.builder()
                .id(0)
                .build());
    }
}
